package org.whuims.web.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;

@Controller
@RequestMapping(value = "/admin/profile")
public class PdfUploadController {
    @Autowired
    private HttpServletRequest request;

    @RequestMapping(value = "uploadBasicHead", produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String ajaxUpload(HttpServletRequest request) throws IllegalStateException,
            IOException {
        MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
//        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        String fileName = "";
        String cat = request.getParameter("cat");
        System.out.println(cat);
        String uploadPath = "resource/docs/";
        String path = request.getSession().getServletContext().getRealPath("/") + File.separator + "resource/docs/" + cat + "/";
        File uploadPathFile = new File(path);
        if (!uploadPathFile.exists()) {
            uploadPathFile.mkdir();
        }
        String realPath = "";
        for (Iterator it = multipartRequest.getFileNames(); it.hasNext(); ) {
            String key = (String) it.next();
            MultipartFile mulfile = multipartRequest.getFile(key);
            fileName = mulfile.getOriginalFilename();
            fileName = handlerFileName(fileName);
            File file = new File(path + fileName);
            System.out.println(file.getAbsolutePath());
            mulfile.transferTo(file);
        }
        realPath = "{\"imagePath\":\"" + fileName + "\"}";
        return realPath;
    }

    //文件名称处理
    private String handlerFileName(String fileName) {
        String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String newFileName = df.format(new Date()) + "_" + new Random().nextInt(100000) + "." + fileExt;
        return newFileName;
    }

    //预览，获取图片流
    @RequestMapping(value = "readImage", produces = "text/plain;charset=UTF-8")
    public void readImage(HttpServletRequest request, HttpServletResponse response) {
        String imagePath = request.getSession().getServletContext().getRealPath("/") + request.getParameter("imagePath");
        try {
            File file = new File(imagePath);
            System.out.println(file.length());
            if (file.exists()) {
                DataOutputStream temps = new DataOutputStream(response
                        .getOutputStream());
                DataInputStream in = new DataInputStream(
                        new FileInputStream(imagePath));
                byte[] b = new byte[2048];
                while ((in.read(b)) != -1) {
                    temps.write(b);
                    temps.flush();
                }
                in.close();
                temps.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}